package com.wuzhishan.news.controller;

import com.wuzhishan.news.pojo.BlogArticle;
import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.pojo.PageBean;
import com.wuzhishan.news.service.Impl.articleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GroupController {
    @Autowired
    private articleServiceImpl articleService;

    /**
     * 板块面板分页显示
     * @param id    groupId
     * @param name  groupName
     * @param ord
     * @param page
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("/group/{id}/{name}")
    public Object group(
            @PathVariable String id,
            @PathVariable String name,
            @RequestParam(required = false,defaultValue = "newest") String ord,
            @RequestParam(required = false,defaultValue = "1") String page,
            @RequestParam(required = false,defaultValue = "10") String pageSize,
            HttpServletRequest request){
        BlogUser user = (BlogUser) request.getSession().getAttribute("loginInfo");
        if(user == null){
            //用户未登录，返回首页
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/group/article");
        if ("recommend".equals(name)){
            //使用推荐
            mv = articleService.recommend(id,ord,page,pageSize,user.getId(),mv);
        }else {
            //正常的展示
            mv = articleService.getAll(id,ord,page,pageSize,mv);
        }
        mv.addObject("articleName",name);
        mv.addObject("servletPath",request.getServletPath());
        return mv;
    }

    /**
     * 模糊查询文章
     * @param word
     * @param page
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("/search")
    public Object search(
            String word,
            @RequestParam(required = false,defaultValue = "1") String page,
            @RequestParam(required = false,defaultValue = "10") String pageSize,
            HttpServletRequest request){
        BlogUser user = (BlogUser) request.getSession().getAttribute("loginInfo");
        if(user == null){
            //用户未登录，返回首页
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/search/article");
        //查询文章
        mv = articleService.search(word,page,pageSize,mv);
        mv.addObject("servletPath",request.getServletPath());
        //分页时候提供参数
        mv.addObject("word",word);
        return mv;
    }
}
